﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       PartNoHelper
- 机器名称：       YTE
- 命名空间：       LD.Host.PartNo
- 文 件 名：       PartNoHelper
- 创建时间：       2022/9/8 15:11:57
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.Common;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace LD.Host
{
    public class PartNoHelper
    {
        public string partPath = Path.Combine(Appbus.BasePath, $"Config\\partno.config");
        public event PartNoDelegate PartNoChange;
        public TB_PartNo Config
        {
            get; set;
        }
        private string curPartNo = "LD";
        public string CurrentPartNo
        {
            get
            {
                return curPartNo;
            }
            set
            {
                curPartNo = value;
                Load();
                Cache.SetCache(CONST.PARTNO, value);

                if (Config == null)
                    Config = new TB_PartNo()
                    {
                        Name = value,
                    };
                PartNoChange?.Invoke(Config);
                AppLoader.LoadFlow(FlowType.机种切换);
            }
        }
        public List<TB_PartNo> GetList
        {
            get; set;
        } = new List<TB_PartNo>();
        private void Load()
        {
            string fileName = Path.Combine(Appbus.BasePath, $"Config\\{curPartNo}.config");
            Config = JsonSerializeHelper.Load<TB_PartNo>(fileName);
        }
        public void Save()
        {
            string fileName = Path.Combine(Appbus.BasePath, $"Config\\{curPartNo}.config");
            JsonSerializeHelper.Save(Config, fileName);
        }
        public void SaveList()
        {
            JsonSerializeHelper.Save(GetList, partPath);
        }
        public void CopyPartNo(string sourcePartNo, string targetPartNo)
        {
            string fileName = Path.Combine(Appbus.BasePath, $"Config\\{sourcePartNo}.config");
            var isExistTargetPartNo = getPartNos.Where(p => p.Name == targetPartNo).FirstOrDefault();
            if (!File.Exists(fileName))
            {
                MessageBox.Show($"{Language.GetString("Source File Is Not Exists")}"
                    , $"{Language.GetString("Warning")}"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Warning);
                return;
            }

            if (isExistTargetPartNo == null)
            {
                MessageBox.Show($"{Language.GetString("Target Recipe Is Not Exists")}"
                    , $"{Language.GetString("Warning")}"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Warning);
                return;
            }
            var m_sourcePartNo = JsonSerializeHelper.Load<TB_PartNo>(fileName);
            if (m_sourcePartNo == null)
            {
                MessageBox.Show($"{Language.GetString("Source Recipe Is Not Setting")}"
                    , $"{Language.GetString("Warning")}"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Warning);
                return;
            }
            var m_targetPartNo = m_sourcePartNo.Clone;
            m_targetPartNo.Name = isExistTargetPartNo.Name;
            m_targetPartNo.Id = isExistTargetPartNo.Id;
            string saveFilePath = Path.Combine(Appbus.BasePath, $"Config\\{targetPartNo}.config");
            JsonSerializeHelper.Save(m_targetPartNo, saveFilePath);
        }
        public List<TB_PartNo> getPartNos
        {
            get
            {
                return JsonSerializeHelper.Load<List<TB_PartNo>>(partPath);
            }
        }
    }
}
